/***************************************************************************
 *   Copyright (C) 2007 by Sebastian Bilski, Lance Cooper, Jeff Rebacz,
 *   									and Alex Zelenskiy
 *   sbilski@iit.edu
 *   lance.m.cooper@gmail.com
 *   jeff.rebacz@gmail.com
 *   voiceofanarchy@gmail.com
 *
 *	This file is part of connect3
 *
 *   connect3 is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2,
 *    as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc., 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 ***************************************************************************/

import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Properties;

import javax.swing.JOptionPane;

/*
 * GUI_CreateAccount.java
 *
 * Created on December 5, 2007, 10:58 AM
 */


public class GUI_Create_Account extends javax.swing.JDialog {
	//Variables declaration - do not modify
	private Color newColor;

	private javax.swing.JLabel blue_Amount;
	private javax.swing.JButton button_Cancel;
	private javax.swing.JButton button_Random;
	private javax.swing.JButton button_ResetFields;
	private javax.swing.JButton button_Save;
	private javax.swing.JLabel green_Amount;
	private javax.swing.JFormattedTextField jFormattedTextField1;
	private javax.swing.JTextPane preview_Color;
	private javax.swing.JLabel label_Blue;
	private javax.swing.JLabel label_Password;
	private javax.swing.JLabel label_Password2;
	private javax.swing.JLabel label_Preview;
	private javax.swing.JLabel label_Red;
	private javax.swing.JLabel label_green;
	private javax.swing.JLabel label_name;
	private javax.swing.JScrollPane preview_Box;
	private javax.swing.JLabel red_Amount;
	private javax.swing.JSlider slider_Blue;
	private javax.swing.JSlider slider_Green;
	private javax.swing.JSlider slider_Red;
	private javax.swing.JTextField text_Name;
	private javax.swing.JPasswordField text_Password;
	private javax.swing.JPasswordField text_Password2;
	// End of variables declaration

	/** Creates new form GUI_CreateAccount */
	public GUI_Create_Account(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();

		newColor=new Color(0);
	}



	private void initComponents() {

		jFormattedTextField1 = new javax.swing.JFormattedTextField();
		label_name = new javax.swing.JLabel();
		label_Password = new javax.swing.JLabel();
		label_Password2 = new javax.swing.JLabel();
		label_Red = new javax.swing.JLabel();
		label_green = new javax.swing.JLabel();
		label_Blue = new javax.swing.JLabel();
		label_Preview = new javax.swing.JLabel();
		text_Name = new javax.swing.JTextField();
		text_Password = new javax.swing.JPasswordField();
		text_Password2 = new javax.swing.JPasswordField();
		slider_Red = new javax.swing.JSlider();
		slider_Green = new javax.swing.JSlider();
		slider_Blue = new javax.swing.JSlider();
		red_Amount = new javax.swing.JLabel();
		green_Amount = new javax.swing.JLabel();
		blue_Amount = new javax.swing.JLabel();
		button_Random = new javax.swing.JButton();
		button_Save = new javax.swing.JButton();
		button_Cancel = new javax.swing.JButton();
		button_ResetFields = new javax.swing.JButton();
		preview_Box = new javax.swing.JScrollPane();
		preview_Color = new javax.swing.JTextPane();

		jFormattedTextField1.setText("What is this??"); // NOI18N
		jFormattedTextField1.setName("jFormattedTextField1"); // NOI18N

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setName("Form"); // NOI18N

		label_name.setText("user name:"); // NOI18N
		label_name.setName("user name:"); // NOI18N

		label_Password.setText("password:"); // NOI18N
		label_Password.setName("label_Password"); // NOI18N

		label_Password2.setText("re-type password:"); // NOI18N
		label_Password2.setName("label_Password2"); // NOI18N

		label_Red.setText("red:"); // NOI18N
		label_Red.setName("label_Red"); // NOI18N

		label_green.setText("green:"); // NOI18N
		label_green.setName("label_green"); // NOI18N

		label_Blue.setText("blue:"); // NOI18N
		label_Blue.setName("label_Blue"); // NOI18N

		label_Preview.setText("preview:"); // NOI18N
		label_Preview.setName("label_Preview"); // NOI18N

		text_Name.setText(""); // NOI18N
		text_Name.setName("text_Name"); // NOI18N

		text_Password.setText(""); // NOI18N
		text_Password.setName("text_Password"); // NOI18N

		text_Password2.setText(""); // NOI18N
		text_Password2.setName("text_Password2"); // NOI18N

		slider_Red.setMaximum(255);
		slider_Red.setName("slider_Red"); // NOI18N
		slider_Red.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(javax.swing.event.ChangeEvent evt) {
				slider_RedStateChanged(evt);
			}
		});

		slider_Green.setMaximum(255);
		slider_Green.setName("slider_Green"); // NOI18N
		slider_Green.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(javax.swing.event.ChangeEvent evt) {
				slider_GreenStateChanged(evt);
			}
		});

		slider_Blue.setMaximum(255);
		slider_Blue.setName("slider_Blue"); // NOI18N
		slider_Blue.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(javax.swing.event.ChangeEvent evt) {
				slider_BlueStateChanged(evt);
			}
		});

		red_Amount.setText("0"); // NOI18N
		red_Amount.setName("red_Amount"); // NOI18N

		green_Amount.setText("0"); // NOI18N
		green_Amount.setName("green_Amount"); // NOI18N

		blue_Amount.setText("0"); // NOI18N
		blue_Amount.setName("blue_Amount"); // NOI18N

		button_Random.setText("random color"); // NOI18N
		button_Random.setName("button_Random"); // NOI18N
		button_Random.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				button_RandomActionPerformed(evt);
			}
		});

		button_Save.setText("save"); // NOI18N
		button_Save.setName("button_Save"); // NOI18N
		button_Save.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				button_SaveActionPerformed(evt);
			}
		});

		button_Cancel.setText("cancel"); // NOI18N
		button_Cancel.setName("button_Cancel"); // NOI18N
		button_Cancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				button_CancelActionPerformed(evt);
			}
		});

		button_ResetFields.setText("reset all fields"); // NOI18N
		button_ResetFields.setName("button_ResetFields"); // NOI18N
		button_ResetFields.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				button_ResetFieldsActionPerformed(evt);
			}
		});

		preview_Box.setName("preview_Box"); // NOI18N

		preview_Color.setName("preview_Color"); // NOI18N
		preview_Color.setEditable(false);
		preview_Color.setText("\n");
		preview_Box.setViewportView(preview_Color);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup()
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(label_name)
												.addComponent(label_Password)
												.addComponent(label_Password2)
												.addComponent(label_Red)
												.addComponent(label_green)
												.addComponent(label_Blue))
												.addGap(10, 10, 10)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(layout.createSequentialGroup()
																.addComponent(slider_Blue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(blue_Amount))
																.addGroup(layout.createSequentialGroup()
																		.addComponent(slider_Green, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(green_Amount))
																		.addGroup(layout.createSequentialGroup()
																				.addComponent(slider_Red, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																				.addComponent(red_Amount))
																				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
																						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
																								.addComponent(text_Name, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
																								.addGroup(layout.createSequentialGroup()
																										.addComponent(button_ResetFields)
																										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(button_Cancel)
																										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
																										.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
																												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(text_Password, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
																														.addComponent(text_Password2, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))))
																														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																														.addComponent(button_Save))))
																														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
																																.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
																																		.addComponent(label_Preview)
																																		.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addComponent(preview_Box))
																																		.addComponent(button_Random, javax.swing.GroupLayout.Alignment.LEADING)))
																																		.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(label_name)
								.addComponent(text_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(label_Password)
										.addComponent(text_Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(label_Password2)
												.addComponent(text_Password2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(label_Red)
														.addComponent(slider_Red, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(red_Amount))
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
																.addComponent(label_green)
																.addComponent(slider_Green, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(green_Amount, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
																		.addComponent(label_Blue)
																		.addComponent(slider_Blue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(blue_Amount))
																		.addGap(18, 18, 18)
																		.addComponent(button_Random)
																		.addGap(18, 18, 18)
																		.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(layout.createSequentialGroup()
																						.addComponent(label_Preview)
																						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
																						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																								.addComponent(button_Save)
																								.addComponent(button_Cancel)
																								.addComponent(button_ResetFields)))
																								.addComponent(preview_Box, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
																								.addContainerGap())
		);
		button_RandomActionPerformed(null);
		pack();
	}// </editor-fold>

	private void button_RandomActionPerformed(java.awt.event.ActionEvent evt) {
		slider_Red.setValue((int)(Math.random()*256));       
		slider_Green.setValue((int)(Math.random()*256));       
		slider_Blue.setValue((int)(Math.random()*256));       
	}

	private void button_ResetFieldsActionPerformed(java.awt.event.ActionEvent evt) {
		text_Name.setText("");
		text_Password.setText("");
		text_Password2.setText("");
	}

	private void button_CancelActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
	}

	private void button_SaveActionPerformed(java.awt.event.ActionEvent evt) {
		
		String userName=text_Name.getText();
		
		char[] field1=text_Password.getPassword();
		char[] field2=text_Password2.getPassword();
		String password1 = new String(field1);
		String password2 = new String(field2);

		if(userName.length()==0){
			JOptionPane.showMessageDialog(this,"user name must be at least 1 character.","username error", JOptionPane.DEFAULT_OPTION);
			return;
		}

		if(password1.length()<6){
			JOptionPane.showMessageDialog(this,"password must be at least 6 characters long.","passwrd error", JOptionPane.DEFAULT_OPTION);
			return;
		}

		if(!password1.equals(password2)){
			JOptionPane.showMessageDialog(this,"passwords do not match.","password error", JOptionPane.DEFAULT_OPTION);
			return;
		}

		
		//save configuration file

		ClientController.config = new Properties();
		ClientController.config.setProperty("name", userName);
		ClientController.config.setProperty("color", ""+newColor.getRGB());
		ClientController.config.setProperty("password", password1);
		
		
		try {
			ClientController.config.store(new FileWriter(userName+".config"),"player data");
		} catch (Exception e1) {
			System.err.println("failed to save configuration file: " + e1.getMessage());
			System.exit(1);
		}
		ClientController.localPlayer.setColor(newColor);
		ClientController.localPlayer.setUserName(userName);
		
		dispose();
		


	}

	private void slider_RedStateChanged(javax.swing.event.ChangeEvent evt) {
		red_Amount.setText(""+slider_Red.getValue());

		newColor=new Color(slider_Red.getValue(),slider_Green.getValue(),slider_Blue.getValue());
		preview_Color.setBackground(newColor);

		preview_Box.repaint();

	}

	private void slider_GreenStateChanged(javax.swing.event.ChangeEvent evt) {
		green_Amount.setText(""+slider_Green.getValue());

		newColor=new Color(slider_Red.getValue(),slider_Green.getValue(),slider_Blue.getValue());
		preview_Color.setBackground(newColor);
	}

	private void slider_BlueStateChanged(javax.swing.event.ChangeEvent evt) {
		blue_Amount.setText(""+slider_Blue.getValue());

		newColor=new Color(slider_Red.getValue(),slider_Green.getValue(),slider_Blue.getValue());
		preview_Color.setBackground(newColor);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				GUI_Create_Account dialog = new GUI_Create_Account(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}



}
